

#import "Leonardo.h"

// This is a singleton class, see below
static Leonardo *sharedLeonardo = nil;

@implementation Leonardo
- (id)init {
	self = [super init];
	server=@"http://test.livetrack24.com/";
	user=@"bilibou";
	pass=@"mel010806";
	return self;
}

-(void)generateSID {
	// we make an int with leftmost bit=1 ,
	// the next 7 bits random 
	// (so that the same userID can have multiple active sessions)
	// and the next 3 bytes the userID
	srandom(time(NULL));
	sid = (random() &  0xFF000000 ) | ( uid & 0x00ffffff) | 0x80000000; 
}

-(void)startSession {
	NSString *urlString =[NSString stringWithFormat:@"%@client.php?op=login&user=%@&pass=%@",server,user,pass];
	NSURL *startSess = [NSURL URLWithString:urlString];
	NSURLRequest *request = [NSURLRequest requestWithURL:startSess
											 cachePolicy:NSURLRequestReturnCacheDataElseLoad
										 timeoutInterval:5];
    NSData *urlData;
	NSHTTPURLResponse *response;
    NSError *error;
	urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	char buffer[40960];
	[urlData getBytes:buffer];   
	uid=atoi(buffer);
}

//Function to tell Leonado server that tracking is starting
-(void)startTracking {
	leolive = 2;
	pid = 1;
	client = @"ParagliderDeck";
	v = 0.1;
	trk1 = 4;
	vtype = 1; //paraglider
	phone = @"iPhone";
	gps = @"internal";
	vname = @"Swing";
	NSString *urlString = [NSString stringWithFormat:@"%@track.php?leolive=%d&sid=%d&pid=%d&client=%@&v=%f&user=%@&pass=%@&phone=%@&gps=%@&trk1=%d&vtype=%d&vname=%@",server,leolive,sid,pid,client,v,user,pass,phone,gps,trk1,vtype,vname];
	NSURL *startTrk = [NSURL URLWithString:urlString];
	NSURLRequest *request = [NSURLRequest requestWithURL:startTrk
											 cachePolicy:NSURLRequestReturnCacheDataElseLoad
										 timeoutInterval:5];
	NSData *urlData;
	urlData = [NSURLConnection connectionWithRequest:request
											delegate:self];
}

//Function to tell Leonardo server that tracking is stoped
-(void)stopTracking {
	leolive = 3;
	prid = 0; //everything is fine
	NSString *urlString = [NSString stringWithFormat:@"%@track.php?leolive=%d&sid=%d&pid=%d&prid=%d",server,leolive,sid,pid,prid];
	NSURL *startTrk = [NSURL URLWithString:urlString];
	NSURLRequest *request = [NSURLRequest requestWithURL:startTrk
											 cachePolicy:NSURLRequestReturnCacheDataElseLoad
										 timeoutInterval:5];
	NSData *urlData;
	urlData = [NSURLConnection connectionWithRequest:request
											delegate:self];
	
}

//Function to send a position to Leonardo server
- (void)newLocationUpdate {
	leolive = 4;
	Location *currentLocation = [[[CLController sharedInstance] locationHistoric] objectAtIndex:0];
	double lat = [currentLocation latitude];
	double lon = [currentLocation longitude];
	int alt = [currentLocation altitude];
	int sog = [currentLocation groundSpeedKMH];
	int cog = [currentLocation course];
	int tm = [[currentLocation timestamp] timeIntervalSince1970];
	NSString *urlString = [NSString stringWithFormat:@"%@track.php?leolive=%d&sid=%d&pid=%d&lat=%f&lon=%f&alt=%d&sog=%d&cog=%d&tm=%d",server,leolive,sid,pid,lat,lon,alt,sog,cog,tm];
	NSURL *startTrk = [NSURL URLWithString:urlString];
	NSURLRequest *request = [NSURLRequest requestWithURL:startTrk
											 cachePolicy:NSURLRequestReturnCacheDataElseLoad
										 timeoutInterval:5];
	NSData *urlData;
	urlData = [NSURLConnection connectionWithRequest:request
											delegate:self];
	
}

- (void)newError:(NSString *)text {
	
}

#pragma mark ---- singleton object methods ----

// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info

+ (Leonardo *)sharedInstance {
    @synchronized(self) {
        if (sharedLeonardo == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return sharedLeonardo;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedLeonardo == nil) {
            sharedLeonardo = [super allocWithZone:zone];
            return sharedLeonardo;  // assignment and return on first allocation
        }
    }
    return nil; // on subsequent allocation attempts return nil
}

+ (BOOL)started {
	if (sharedLeonardo == nil) return FALSE;
	else return TRUE;
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain {
    return self;
}

- (unsigned)retainCount {
    return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
}

- (id)autorelease {
    return self;
}


@end
